Ensuring thread affinity for interprocess communication in a managed code environment

ABSTRACT

A remote procedure call channel for interprocess communication in a managed code environment ensures thread-affinity on both sides of an interprocess communication. Using the channel, calls from a first process to a second process are guaranteed to run on a same thread in a target process. Furthermore, calls from the second process back to the first process will also always execute on the same thread. An interprocess communication manager that allows thread affinity and reentrancy is able to correctly keep track of the logical thread of execution so calls are not blocked in unmanaged hosts. Furthermore, both unmanaged and managed hosts are able to make use of transparent remote call functionality provided by an interprocess communication manager for the managed code environment.

BACKGROUND

It is commonplace to extend the functionality of an existing applicationsuch as MICROSOFT EXCEL® by developing an “add-in”. An add-in istypically not written by the creators of the host application but bythird party developers that want to extend the functionality of the hostapplication. An add-in can be created with development tools such asVISUAL STUDIO .NET 2005® and VISUAL STUDIO 2005 TOOLS FOR APPLICATIONS®.An add-in is a component that a host application such as MICROSOFTEXCEL® loads when it is needed such as when the host application startsup or when a particular document is loaded by the host application.

A host application may choose to execute an add-in in a separate processfor several reasons including process stability, platform versionresilience, or special debugging scenarios. Running an add-in in aseparate process requires a mechanism for the host application and theadd-in to communicate across process boundaries. There exist severalRemote Procedure Call (RPC) technologies a host may use for thiscommunication. However, there are several problems specific to thehost/add-in model that require special considerations that most RPCtechnologies do not consider. The most notable of these problems isthreading model of the host and of the add-in.

MICROSOFT® .NET is an example of a set of technologies that provide amanaged code environment. .NET managed code has access to all theservices that the Common Language Runtime (CLR) technology makesavailable, such as cross-language integration, security and versioningsupport, and garbage collection. Code that does not operate within aruntime managed framework such as CLR is called unmanaged code.

In the .NET environment, the standard RPC technology is .NET Remoting..NET Remoting provides an interprocess communication manager for managedcode. NET Remoting makes remote procedure calls transparent, i.e.,invisible, to the caller/callee. That is, a first object may call amethod on a second object which exists in a separate process without thefirst object knowing that the call is remote. .NET Remoting interceptsthe call to the second object via a proxy and transports that call tothe other process. However, in the current .NET Remoting implementationeach call across process executes on a different thread in the targetprocess. This can cause many problems for add-ins which normally run ona specific thread within the host application. If the add-in uses atechnology which expects thread affinity, that technology will fail whenthe add-in is moved to an external process and called from multipleremoting threads. An example of such a technology is MICROSOFT WINDOWS®Timers.

A related problem occurs in the multitudes of unmanaged applicationswritten using, for example, MICROSOFT's® Common Object Model (COM)technology. COM technologies provide, among other things, aninterprocess communication manager for unmanaged code. In COMapplications, it is common practice for a thread within the applicationto be marked as a “Single-Threaded Apartment”. Any call into objectswithin this apartment must first be marshaled onto the apartment'sthread (i.e. transferred to execute on the thread contained within theapartment). COM keeps track of the incoming and out-going calls withinthe apartment. The collection of these calls is called the logicalthread. When a new call enters the apartment, this call is called the“top-level call” of the logical thread. If a call leaves the apartmentby calling an object outside the apartment boundaries (eithercross-process or cross apartment) incoming top-level calls will beblocked by COM. If the out-going call results in a new call coming in,that call is part of the current logical thread and is allowed to enterthe apartment. Only calls which are part of the current logical threadare allowed to re-enter the apartment. This means the current .NETRemoting technology is unacceptable for communication between a COM hostand its add-ins. If the corn host was to make a call to an add-in using.NET Remoting, and that call executed on a different thread in theadd-in, calls back to the host would appear to be a different top-levelcall to COM and would be rejected.

SUMMARY

In consideration of the above-identified shortcomings of the art, thepresent invention ensures thread affinity for interprocess communicationin a managed code environment. It provides a remote procedure callchannel for interprocess communication in a managed code environmentwhich ensures thread-affinity on both sides of an interprocesscommunication. Using the channel, calls from a first process to a secondprocess, such as from a host to an add-in, are guaranteed to run on asame thread in a target process. Furthermore, calls from the add-in tothe host will also always execute on the same thread. An interprocesscommunication manager that supports thread affinity and reentrancy isable to correctly keep track of the logical thread of execution so callsare not blocked in unmanaged hosts. Furthermore, both unmanaged andmanaged hosts are able to make use of transparent remote callfunctionality provided by an interprocess communication manager for themanaged code environment. Other advantages and features of the inventionare described below.

BRIEF DESCRIPTION OF THE DRAWINGS

The systems and methods for ensuring thread affinity for interprocesscommunication in a managed code environment in accordance with thepresent invention are further described with reference to theaccompanying drawings in which:

FIG. 1 provides a diagram of a remote procedure call channel forinterprocess communication which uses an interprocess communicationmechanism that allow thread affinity and re-entrancy.

FIG. 2 illustrates an exemplary advantageous use of the invention inwhich a host 202 is painting while an add-in 204 is being debugged.

DETAILED DESCRIPTION

Certain specific details are set forth in the following description andfigures to provide a thorough understanding of various embodiments ofthe invention. Certain well-known details often associated withcomputing and software technology are not set forth in the followingdisclosure, however, to avoid unnecessarily obscuring the variousembodiments of the invention. Further, those of ordinary skill in therelevant art will understand that they can practice other embodiments ofthe invention without one or more of the details described below.Finally, while various methods are described with reference to steps andsequences in the following disclosure, the description as such is forproviding a clear implementation of embodiments of the invention, andthe steps and sequences of steps should not be taken as required topractice this invention.

In one embodiment, interprocess communication in a managed codeenvironment can be implemented using .NET Remoting technology. While thespecific embodiment described herein is explained with reference to .NETRemoting, the invention is not limited to such embodiments. Referring toFIG. 1, .NET Remoting is architected around the concept of a channel110. A channel comprises one or more sinks which each have a specificjob. Each sink takes a cross-process message, processes it in some way,and hands it to the next sink. Thus, a message may travel from theobject making a remote call 121 to the transparent proxy 126, then to aFormatSink 127, then to a StackBuilderSink 128. This continues until thetransport sink 129 is reached. The transport sink 129 is responsible foractually sending the constructed message to the target process 130. Theexisting .NET Remoting sinks, e.g. 129 send this message using networkprotocols such as HTTP and TCP or using Microsoft Windows Named Pipes.However, the receiving end of these transport sinks use a differentthread-pool thread for incoming messages, resulting in the problemsdescribed above. Thus, in one embodiment, custom sinks 129,139 can pluginto channel 110 at the incoming messages thread, and transport amessage across process boundaries with thread affinity.

Thus, .NET Remoting provides a framework that allows objects to interactwith one another across process boundaries. The framework mayadditionally provide a number of services, including activation andlifetime support. Formatters such as 127 and 137 are used for encodingand decoding the messages before they are transported by the channel.Applications can use binary encoding where performance is critical, orXML encoding where interoperability with other remoting frameworks isessential. XML encoding generally uses the SOAP protocol in transportingmessages from one process 120 to the other 130. It will be understoodthat references herein to industry standard specifications such as XMLand SOAP refer to any version of such specifications presently in use orlater developed. Remoting was designed with security in mind, and anumber of hooks are provided that allow channel sinks to gain access tothe messages and serialized stream before the stream is transported overthe channel.

Proxy objects such as 126 are created when a originating object 121activates a remote object 131. The proxy object 126 acts as arepresentative of the remote object 131 and ensures that all calls madeon the proxy 126 are forwarded to the correct remote object instance.When a originating object 121 activates a remote object 131, theframework creates a local instance of the class TransparentProxy thatcontains a list of all classes, as well as interface methods of theremote object. Since the TransparentProxy class is registered with theCLR when it gets created, all method calls on the proxy are interceptedby the runtime. Here the call is examined to determine if it is a validmethod of the remote object and if an instance of the remote objectresides in the same application domain as the proxy. If this is true, asimple method call is routed to the actual object. If the object is in adifferent application domain, the call parameters on the stack arepackaged into an IMessage object and forwarded to a RealProxy class bycalling its Invoke method. This class (or rather an internalimplementation of it) is responsible for forwarding messages to theremote object. Both the TransparentProxy and RealProxy classes arecreated under the covers when a remote object is activated, but only theTransparentProxy is returned to the client.

Channels are used to transport messages to and from remote objects suchas 131. When a originating object 121 calls a method on a remote object131, the parameters, as well as other details related to the call, aretransported through the channel 110 to the remote object 131. Anyresults from the call are returned back to the originating object 121 inthe same way. An originating object 121 can select any of the channelsregistered on the “server” to communicate with the remote object 131,thereby allowing developers the freedom to select the channels that bestsuit their needs. It is also possible to customize any existing channelor build new ones that use different communication protocols. In oneembodiment of the invention, a new .NET Remoting channel is built thatoperates using an interprocess communication mechanism that allowsthread affinity and reentrancy 140, such as COM RPC.

In .NET Remoting, custom channels can be supported using channelservices, which are pluggable using an interface called IChannel. Thefollowing provides sample code to load .NET channel services, for anexemplary new http or tcp channel, which can be extended by those ofskill to a channel associated with any communications protocol:

Sample Code To Load .NET Channel Services: public class myRemotingObj {HttpChannel httpChannel; TcpChannel tcpChannel; public voidmyRemotingMethod( ) { httpChannel = new HttpChannel( ); tcpChannel = newTcpChannel( ); ChannelServices.RegisterChannel(httpChannel); // Registerthe HTTP Channel ChannelServices.RegisterChannel(tcpChannel); //Register the TCP Channel } }

All .NET Remoting channels derive from IChannel and implement eitherIChannelReceiver or IChannelSender, depending on the purpose of thechannel. Most channels implement both the receiver and sender interfacesto enable them to communicate in either direction. When a originationgobject 121 calls a method on a proxy 126, the call is intercepted by theremoting framework and changed into a message that is forwarded to theRealProxy class (or rather, an instance of a class that implementsRealProxy). The RealProxy forwards the message to the channel sink chainfor processing.

This first sink in the chain can be a formatter sink 127 that serializesthe message into a stream of bytes. The message is then passed from onechannel sink to the next until it reaches the transport sink 129 at theend of the chain. The transport sink 129 is responsible for establishinga connection with the transport sink 139 on the server side and sendinga byte stream to the server. The transport sink 139 on the server thenforwards the byte stream through the sink chain on the server side 135until it reaches the formatter sink 137, at which point the message isdeserialized from its point of dispatch to the remote object itself.

Managed code interprocess communication channels, such as .NET Remoting,can be employed in a variety of scenarios. For example, .NET Remotingobjects can be exposed as a Web Service by hosting them in IIS. It isalso possible to call Unmanaged Classic COM components through COMInterop Services. When the .NET Remoting client object creates aninstance of a COM object, the object is exposed through a runtimecallable wrapper (RCW) that acts as a proxy for the real unmanagedobject. These wrappers appear to be just like any other managed class tothe .NET client, but in actuality they just marshal calls betweenmanaged (.NET) and unmanaged (COM) code.

Similarly a .NET Remoting server object can be exposed to classic COMclients. When a COM client creates an instance of the .NET object, theobject is exposed through a COM callable wrapper (CCW) that acts as aproxy for the real managed object.

Referring again to FIG. 1, in one embodiment, a remote procedure callchannel 110 is provided comprising client side objects 125 and serverside objects 135. The remote procedure call channel 110 may be a custom.NET Remoting channel. The channel 110 uses an interprocesscommunication mechanism that allows thread affinity and reentrancy 140.One such communication mechanism is COM RPC, an existing interprocesscommunication manager for unmanaged code.

In FIG. 1, messages flow from the object making a remote call, referredto herein as the originating object 121, down the stack through clientside interprocess communication objects 125 to the transport sink 129.Transport sink 129 is a client side managed code interprocesscommunication object, as are objects 126, 127, and 128. Objects 137 and138 are server side managed code interprocess communication objects.

A thread-affinitized COM object 139 receives a message from 129 via 140and sends it up the stack, through server side interprocesscommunication objects 135 to the destination object 131. A call from anoriginating object 121 in Process 1 120 to a destination object 131 inProcess 2 130 will always be received by thread affinitized object 139.“Thread affinitized” is used here as it is understood in the art, thatis, a thread affinitized object cannot be communicated with via someother thread than that which the thread affinitized object itself usesfor communications. A thread affinitized object is often also asingle-threaded object.

Process 120 may be a host process and process 130 may be an add-inprocess. The .NET Remoting channel 110 in FIG. 1 ensures thread-affinityon both sides of the interprocess communication between host 120 andadd-in 130. Calls from the host 120 to the add-in 130 are guaranteed torun on the same thread in the target process 130. Furthermore, calls inthe other direction (from the add-in 130 to the host 120) will alsoalways execute on the same thread if the add-in communicates back to thehost on a channel that implements the invention. Thus, technologies suchas WINDOWS® Timers work as expected regardless of whether an add-in isrunning in a separate process from its host. Interprocess communicationsmechanisms such as COM are able to correctly keep track of the logicalthread of execution so calls are not blocked in unmanaged hosts, therebysupporting reentrancy. Furthermore, both unmanaged and managed hosts areable to make use of .NET Remoting's transparent remote callfunctionality.

In one embodiment, a piece of computer software, i.e. a computer programthat implements a custom remote procedure call channel for interprocesscommunication 110 can plug into a managed code interprocesscommunication channel such as the .NET Remoting technology of 125,135 toguarantee thread-affinity on each side of a process boundary.Cross-process calls (the noun “call” and the noun “message” are usedinterchangeably herein) between managed code interprocess communicationobjects such as .NET objects 121 and 131 work transparently regardlessof the threading model of the host 120 or add-in 130. Furthermore,add-ins 130 are able to work with the host's 120 .NET object modelwithout directly relying on an external unmanaged cross-processcommunication technology such as COM RPC. COM RPC would require asubstantially different programming model when the add-in 131 is runningin process 120 than when the add-in 131 is running outside of process120. Thus a combination of technologies can be used to attain the goalsof cross-process object thread-affinity while remaining transparent to.NET technologies.

The custom .NET Remoting channel 110 uses a thread-affinitized COMobject 139 for the actual transportation of messages between the twoprocesses 120, 130. Existing .NET Remoting technology 129 converts amessage to a destination object 131 into a collection of bits thatdescribe the message. The collection of bits can be either SOAP (SimpleObject Access Protocol) or .NET Remoting's binary format. Rather thantransporting this message to process 130 using a network protocol suchas HTTP or TCP (as .NET Remoting currently does), the message is passedacross process boundaries to a thread affinitized COM object 139 in theother process 130. This COM object 139 gives the .NET Remoting messageto the Remoting infrastructure 135 on the server side 130 which thendispatches the message to the appropriate object 131.

Since thread affinitized COM object 139 can only receive messages on onethread, the incoming cross-process message executes on the one and onlythread in the apartment corresponding to the thread of object 139. Thisremoting channel 110 effectively gives thread affinity to the object 139called across process. Furthermore, since the outgoing message appearsto be a standard COM cross-process call, COM's logical thread systemremains intact and unmanaged COM hosts (in one embodiment originatingobject 121 may be an unmanaged host object) are able to use .NETRemoting objects 125 instead of using COM RPC 140 directly.

One advantage of the COM RPC channel over the existing channels is theability for a host to install a message filter into a single threadedapartment to selectively control re-entrancy. Hosts using the COM RPCchannel have the ability to pump windows messages while waiting for acall to return from the add-in. This allows the host to paint its userinterface even while waiting for a call to the add-in to return. Anexample of this is illustrated in FIG. 2. This works even if the add-inprocess is being debugged and has been suspended by the debugger. Theexisting remoting channels do not have support for this scenario withoutexclusively using asynchronous calls. The state of the host process'suser interface is very important to add-in developers becauseinformation about it may not be available in the debugger. A goodexample of such information in the user interface is the contents ofcells in a MICROSOFT EXCEL® worksheet. Add-in developers have a mucheasier time debugging MICROSOFT EXCEL® add-ins if they are able to seethe contents of the workbook while debugging.

In one embodiment, a message filter is a COM object which implements theIMessageFilter interface. COM RPC provides various notifications to themessage filter, allowing the host to override some of the defaultbehavior of COM. One such notification is MessagePending. MessagePendingis sent when a call has exited the single threaded apartment and awindows message is waiting to be processed by the apartment's messageloop. An add-in host using the COM RPC channel can register a messagefilter, and respond to MessagePending by pumping WM_PAINT, thus updatingits user interface.

FIG. 2 demonstrates the host 202 painting while an add-in 204 is beingdebugged. 1) The host 202 creates the message filter 201. 2) The host202 makes a call to the add-in 204 via the COM RPC remoting channel 203.3) The call is transmitted to the add-in 204. 4) The debugger 205suspends the execution of the add-in 204 process. 5) At some later time(while the debugger 205 still has the add-in 204 process suspended) theoperating system 200 decides it is time for the host 202 to repaint itsUI. Since there is a call outside the apartment, this message goes tothe message filter 201 as MessagePending. 6) The message filter 201tells the host 202 to repaint its UI. 7) The host 202 repaints. At somelater time, the debugger 205 allows the add-in 204 process to continueexecution.

In addition to the specific implementations explicitly set forth herein,other aspects and implementations will be apparent to those skilled inthe art from consideration of the specification disclosed herein. It isintended that the specification and illustrated implementations beconsidered as examples only, with a true scope and spirit of thefollowing claims.

1. A remote procedure call channel for interprocess communication whichensures thread-affinity on both sides of said interprocesscommunication, comprising: a client side managed code interprocesscommunication object configured to convert a message into a collectionof bits and to send said message across a process boundary to a threadaffinitized server side interprocess communication object using aninterprocess communication mechanism that allows thread affinity andreentrancy; wherein said thread affinitized server side interprocesscommunication object comprises a single thread, and said messageexecutes on said single thread.
 2. The remote procedure call channel forinterprocess communication of claim 1, wherein said message originatesfrom an object associated with a host process and is delivered to anobject associated with an add-in process.
 3. The remote procedure callchannel for interprocess communication of claim 2, wherein said managedcode interprocess communication object comprises a .NET Remoting clienttransport sink.
 4. The remote procedure call channel for interprocesscommunication of claim 1, wherein said thread affinitized server sideinterprocess communication object is a Component Object Model (COM)object.
 5. The remote procedure call channel for interprocesscommunication of claim 4, wherein said interprocess communicationmechanism that allows thread affinity and reentrancy comprises a COMRemote Procedure Call (RPC) interprocess communication mechanism.
 6. Theremote procedure call channel for interprocess communication of claim 1,wherein said collection of bits is in a Simple Object Access Protocol(SOAP) format.
 7. The remote procedure call channel for interprocesscommunication of claim 1, wherein said collection of bits is in a .NETRemoting binary format.
 8. A remote procedure call channel that uses athread affinitized Component Object Model (COM) object for transportingmessages between a first process and a second process, comprising: afirst .NET Remoting object configured to convert a message from anoriginating object into a collection of bits and to send said collectionof bits across a process boundary to a thread affinitized COM object;wherein said thread affinitized COM object is configured to receive saidcollection of bits from said .NET Remoting object and send saidcollection of bits to a second .NET Remoting object; wherein said second.NET Remoting object is configured to send said message to a destinationobject.
 9. The remote procedure call channel of claim 8, wherein saidcollection of bits is in a Simple Object Access Protocol (SOAP) format.10. The remote procedure call channel of claim 9, wherein saidcollection of bits is in a .NET Remoting binary format.
 11. The remoteprocedure call channel of claim 8, wherein said .NET Remoting objectsends said collection of bits via a COM Remote Procedure Call (RPC). 12.A remote procedure call channel for interprocess communication,comprising: a thread affinitized server side interprocess communicationobject configured to receive an interprocess message via an interprocesscommunication mechanism that allows reentrancy, and further configuredto send said message to a destination object via a managed codeinterprocess communication object.
 13. The remote procedure call channelfor interprocess communication of claim 12, wherein said interprocessmessage is from an object associated with a host process, and whereinsaid destination object is associated with an add-in process.
 14. Theremote procedure call channel for interprocess communication of claim12, wherein said managed code interprocess communication objectcomprises a .NET Remoting object.
 15. The remote procedure call channelfor interprocess communication of claim 12, wherein said threadaffinitized server side interprocess communication object is a ComponentObject Model (COM) object.
 16. The remote procedure call channel forinterprocess communication of claim 15, wherein said interprocesscommunication mechanism that allows reentrancy comprises a COM RemoteProcedure Call (RPC) mechanism.
 17. The remote procedure call channelfor interprocess communication of claim 12, wherein said interprocessmessage is received by said thread affinitized server side interprocesscommunication object in a Simple Object Access Protocol (SOAP) format.18. The remote procedure call channel for interprocess communication ofclaim 12, wherein said interprocess message is received by said threadaffinitized server side interprocess communication object in a .NETRemoting binary format.